package com.phamloc.shootingchicken;

import java.io.IOException;

import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.BaseGameActivity;

import android.util.DisplayMetrics;
import android.view.KeyEvent;

import com.phamloc.shootingchicken.manager.ResourcesManager;
import com.phamloc.shootingchicken.manager.SceneManager;
public class GameActivity extends BaseGameActivity{
	private BoundCamera camera;
	public static  int SCREEN_WIDHT = 800;
	public static  int SCREEN_HIGHT = 500;
	@Override
	public EngineOptions onCreateEngineOptions() {
		DisplayMetrics display = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(display);
		SCREEN_WIDHT = display.widthPixels;
		SCREEN_HIGHT = display.heightPixels;
		camera = new BoundCamera(0, 0, SCREEN_WIDHT, SCREEN_HIGHT);
		final EngineOptions engineOptions = new EngineOptions(true, 
		ScreenOrientation.PORTRAIT_FIXED,
		new RatioResolutionPolicy(SCREEN_WIDHT, SCREEN_HIGHT), camera);
		engineOptions.getAudioOptions().setNeedsSound(true).setNeedsMusic(true);
		return engineOptions;
	}

	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws IOException {
		ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws IOException {
		SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback)
			throws IOException {
		mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				mEngine.unregisterUpdateHandler(pTimerHandler);
				SceneManager.getInstance().createMenuScene();
			}
		}));
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.exit(0);
	}
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		System.exit(0);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK)
	    {
	        SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
	    }
	    return false; 
	}
}
